package cn.huanba.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.URL;
import java.util.Date;

/**
 * 阿里oss服务器
 */
//@ConfigurationProperties(prefix = "oss")
@Component
public class OssUtils {
    private String endpoint = "oss-cn-qingdao.aliyuncs.com";
    private String accessKeyId = "LTAI4G7Ne36FWXZQhMj58LQq";
    private String accessKeySecret = "uQtGKmXwFlHdJtKDryvgZvHTburmwj";
    private String bucketName = "huanleba";

    /***
     * 文件上传
     * @param fileName
     * @param file
     */
    public void fileUpload(String path, String fileName, File file) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, path + fileName, file);
    }

    /**
     * 临时访问路径生成
     *
     * @param objectName
     * @return
     */
    public String createFileName(String objectName) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //此链接的有效期
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
        URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
        return url.toString();
    }

}

